/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.mine.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.impl.util.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.mine.entity.Hole;
import com.thinkgem.jeesite.modules.mine.entity.Point;
import com.thinkgem.jeesite.modules.mine.service.HoleService;
import com.thinkgem.jeesite.modules.mine.service.PointService;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;

/**
 * 采集点信息Controller
 * @author developer
 * @version 2018-11-05
 */
@Controller
@RequestMapping(value = "${adminPath}/mine/point")
public class PointController extends BaseController {
	@Autowired
	private HoleService holeService;
	@Autowired
	private PointService pointService;

	
	@ModelAttribute
	public Point get(@RequestParam(required=false) String id) {
		Point entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = pointService.get(id);
		}
		if (entity == null){
			entity = new Point();
		}
		return entity;
	}
	
	@RequiresPermissions("mine:point:view")
	@RequestMapping(value = {"list", ""})
	public String list(Point point, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Point> page = pointService.findPage(new Page<Point>(request, response), point); 
		List<Point> list = page.getList();
		for (Point p : list) {
			p.setHoleId(holeService.get(p.getHoleId()).getName());
		}
		model.addAttribute("page", page);
		return "modules/mine/pointList";
	}

	@RequiresPermissions("mine:point:view")
	@RequestMapping(value = "form")
	public String form(Point point, Model model) {
		model.addAttribute("point", point);
		return "modules/mine/pointForm";
	}

	@RequiresPermissions("mine:point:edit")
	@RequestMapping(value = "save")
	public String save(Point point, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, point)){
			return form(point, model);
		}
		pointService.save(point);
		addMessage(redirectAttributes, "保存采集点信息成功");
		return "redirect:"+Global.getAdminPath()+"/mine/point/?repage";
	}
	
	@RequiresPermissions("mine:point:edit")
	@RequestMapping(value = "delete")
	public String delete(Point point, RedirectAttributes redirectAttributes) {
		pointService.delete(point);
		addMessage(redirectAttributes, "删除采集点信息成功");
		return "redirect:"+Global.getAdminPath()+"/mine/point/?repage";
	}
	
	@RequiresPermissions("mine:point:edit")
	@RequestMapping(value = "savePoint")
	@ResponseBody
	public String savePoint(HttpServletRequest request, HttpServletResponse response,Point point) throws Exception {	
		Hole hole= holeService.get(point.getHoleId());
		point.setRoadwayId(hole.getRoadwayId());
		JSONObject job = new JSONObject();
		if(point.getPosition()>holeService.get(point.getHoleId()).getLength()) {
			return job.accumulate("error", "保存失败，采集点长度超过探测空长度，数据无效，请检查！").toString();

		}
			pointService.save(point);
			job.accumulate("success", "采集点成分信息保存成功！");
			return job.toString();
	}
	
	@RequiresPermissions("mine:point:view")
	@RequestMapping(value = "findByHoleId")
	@ResponseBody
	public List<Point> findByHoleId(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List <Point>list = pointService.findListByHoleId(request.getParameter("holeId"));
		for (Point point : list) {
			point.setComposition(DictUtils.getDictLabel(point.getComposition(),"probe_type", ""));
		}
		return list ;
	}
	
}